if exists(select name from sysobjects where name = 'sp_SanPhamGetSPCungLoai')
	drop proc sp_SanPhamGetSPCungLoai
go
/*-- =============================================
	Created by: HP
	Created on: 2014-03-15 (dd/mm/yyyy)
	Description: 
	--

	File:$sql-scripts/Proc.sp_SanPhamGetSPCungLoai.sql
*/-- =============================================

CREATE PROCEDURE sp_SanPhamGetSPCungLoai
@Id int
AS
BEGIN
	--lay san loai san pham
	declare @LoaiSanPham int =(select LoaiSanPhamId from SanPham where ID=@Id)
	select * from (SELECT top 3 a.[Id],						a.[TenSanPham],		a.[MoTa],		a.[NoiDung],		a.[GiaBan],		a.[GiaKM],		a.[ThuTu],		a.[Image],		a.[Image1],		a.[Image2],		a.[Image3],		a.[Image4],		a.[Keywords],		a.[Description],		a.[Title],		a.[NguoiTao],		a.[NgayTao],		a.[NguoiSua],		a.[NgaySua],		a.[TrangThai],		a.[LoaiSanPhamId]
			FROM        SanPham a where trangthai<>255 and a.LoaiSanPhamId=@LoaiSanPham and a.Id>@Id order by a.Id )  tlast
	UNION all
	select * from (
		SELECT top 3
		a.[Id],						a.[TenSanPham],		a.[MoTa],		a.[NoiDung],		a.[GiaBan],		a.[GiaKM],		a.[ThuTu],		a.[Image],		a.[Image1],		a.[Image2],		a.[Image3],		a.[Image4],		a.[Keywords],		a.[Description],		a.[Title],		a.[NguoiTao],		a.[NgayTao],		a.[NguoiSua],		a.[NgaySua],		a.[TrangThai],		a.[LoaiSanPhamId]
			FROM        SanPham a where trangthai<>255 and a.LoaiSanPhamId=@LoaiSanPham and a.Id<@Id order by a.Id desc) tfirst
END
GO
